package zzs.generalframework.console.core.user.usereducation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.user.usereducation.UserEducationBO;
import zzs.generalframework.common.core.user.usereducation.UserEducationInfo;
import zzs.generalframework.common.core.user.usereducation.UserEducationPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.user.usereducation.dao.IUserEducationDao;
import zzs.generalframework.console.core.user.usereducation.service.IUserEducationService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.usereducation.service.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.user.usereducation.service.impl.userEducationServiceImpl")
public class UserEducationServiceImpl implements IUserEducationService
{

	private Logger logger = LoggerFactory.getLogger(UserEducationServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IUserEducationDao userEducationDao;

	@Override
	public ConsoleReturnBO saveUserEducation(UserEducationBO userEducationBO)
	{
		logger.debug("====保存用户教育信息开始====");
		logger.debug("====保存用户教育信息.入参:UserEducationBO=[{}]====", userEducationBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserEducationPO userEducationPO = new UserEducationPO();
		copyUserEducationBO2UserEducationPO(userEducationBO, userEducationPO);
		Boolean insertResult = userEducationDao.insert(userEducationPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存用户教育信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户教育信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveUserEducation(List<UserEducationBO> userEducationBOList) throws ConsoleException
	{
		logger.info("====保存用户教育信息列表开始====");
		logger.debug("====保存用户教育信息列表.入参:UserEducationBOList=[{}]====", userEducationBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserEducationBO itemBO : userEducationBOList)
		{
			ConsoleReturnBO itemResult = saveUserEducation(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====保存用户教育信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户教育信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getUserEducation(UserEducationInfo userEducationInfo)
	{
		logger.info("====获取用户教育信息开始====");
		logger.debug("====获取用户教育信息.入参:UserEducationInfo=[{}]====", userEducationInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserEducationPO> userEducationPOList = userEducationDao.select(userEducationInfo);
		if (userEducationPOList == null || userEducationPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (userEducationPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			UserEducationBO userEducationBO = new UserEducationBO();
			copyUserEducationPO2UserEducationBO(userEducationPOList.get(0), userEducationBO);
			returnConsoleReturnBO.setReturnObj(userEducationBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			UserEducationBO userEducationBO = new UserEducationBO();
			copyUserEducationPO2UserEducationBO(userEducationPOList.get(0), userEducationBO);
			returnConsoleReturnBO.setReturnObj(userEducationBO);
		}
		logger.debug("====获取用户教育信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户教育信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listUserEducation(UserEducationInfo userEducationInfo)
	{
		logger.info("====获取用户教育信息列表开始====");
		logger.debug("====获取用户教育信息列表.入参:UserEducationInfo=[{}]====", userEducationInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserEducationPO> userEducationPOList = userEducationDao.select(userEducationInfo);
		if (userEducationPOList == null || userEducationPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<UserEducationBO> resultList = new ArrayList<UserEducationBO>();
			for (UserEducationPO itemPO : userEducationPOList)
			{
				UserEducationBO itemBO = new UserEducationBO();
				copyUserEducationPO2UserEducationBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取用户教育信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户教育信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserEducation(UserEducationBO userEducationBO)
	{
		logger.info("====更新用户教育信息开始====");
		logger.debug("====更新用户教育信息.入参:UserEducationBO=[{}]====", userEducationBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserEducationPO userEducationPO = new UserEducationPO();
		copyUserEducationBO2UserEducationPO(userEducationBO, userEducationPO);
		Boolean updateResult = userEducationDao.update(userEducationPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新用户教育信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户教育信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserEducation(List<UserEducationBO> userEducationBOList) throws ConsoleException
	{
		logger.info("====更新用户教育信息列表开始====");
		logger.debug("====更新用户教育信息列表.入参:List<UserEducationBO>=[{}]====", userEducationBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserEducationBO itemBO : userEducationBOList)
		{
			ConsoleReturnBO itemResult = updateUserEducation(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====更新用户教育信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户教育信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserEducation(UserEducationBO userEducationBO)
	{
		logger.info("====删除用户教育信息开始====");
		logger.debug("====删除用户教育信息.入参:UserEducationBO＝[{}]======", userEducationBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserEducationPO userEducationPO = new UserEducationPO();
		copyUserEducationBO2UserEducationPO(userEducationBO, userEducationPO);
		Boolean deleteResult = userEducationDao.delete(userEducationPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除用户教育信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户教育信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserEducation(List<UserEducationBO> userEducationBOList) throws ConsoleException
	{
		logger.info("====删除用户教育信息列表开始====");
		logger.debug("====删除用户教育信息列表.入参:List<UserEducationBO>＝[{}]======", userEducationBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserEducationBO itemBO : userEducationBOList)
		{
			ConsoleReturnBO itemResult = removeUserEducation(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====删除用户教育信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户教育信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countUserEducation(UserEducationBO userEducationBO)
	{
		logger.info("====统计用户教育信息开始====");
		logger.debug("====统计用户教育信息。入参：UserEducationBO＝[{}]======", userEducationBO);
		Long returnLong = new Long(0);
		UserEducationPO userEducationPO = new UserEducationPO();
		copyUserEducationBO2UserEducationPO(userEducationBO, userEducationPO);
		returnLong = userEducationDao.count(userEducationPO);
		logger.debug("====统计用户教育信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计用户教育信息结束====");
		return returnLong;
	}

	private void copyUserEducationBO2UserEducationPO(UserEducationBO userEducationBO, UserEducationPO userEducationPO)
	{
		beanUtils.copyBeanNotNull(userEducationBO, userEducationPO);
		if (userEducationBO.getSysID() != null)
		{
			userEducationPO.setSeqID(userEducationBO.getSysID());
		}
	}

	private void copyUserEducationPO2UserEducationBO(UserEducationPO userEducationPO, UserEducationBO userEducationBO)
	{
		beanUtils.copyBeanNotNull(userEducationPO, userEducationBO);
		if (userEducationPO.getSeqID() != null)
		{
			userEducationBO.setSysID(userEducationPO.getSeqID());
		}
	}

}
